import {Component, OnInit, Input, SimpleChange, OnChanges} from '@angular/core';

import { ApiService } from "@core/services/http/api.service";
import {ApesMessageService} from "@shared/apes/message";

@Component({
  selector: 'apes-door-control-table',
  templateUrl: './door-control-table.component.html',
  styleUrls: ['./door-control-table.component.scss']
})
export class DoorControlTableComponent implements OnInit , OnChanges{

  @Input()
  set parmas(value: any) {
    this.title = value["title"];
    this.titleList = value["titleList"];
    this.tabWidth = value["tabWidth"];
    this._parmas = value;
  }
  get parmas() {
    return this._parmas;
  }

  @Input() companyId: any;

  _parmas: any;
  title: string;
  tabWidth: string;
  titleList: any[] = [];
  reportData: any[] = [];

  allData: any[] = [];
  tablesIndex: number = 0;
  allList: any = {};

  constructor(private apiService: ApiService,
              private msg: ApesMessageService) {
    const time = (1000 * 60) * 10;
    setInterval(() => {
      this.allData =  [];
      this.getData();
    }, time)

    setInterval(() => {
      this.reportData = [];
      this.pageList(this.allData);
    }, 5000)
  }

  ngOnInit() {
  }

  ngOnChanges(changes: {[propName: string]: SimpleChange}) {
    this.companyId = changes['companyId'].currentValue;
    this.getData();
  }

  getData() {
    if (this.parmas["api"]) {
      this.allData = [];
      let data = { 'interfaceMethod': this.parmas["api"], 'pdata': {'companyId': this.companyId, 'MDLXBJ': this.parmas["MDLXBJ"]} };
      // debugger;
      this.apiService.post('report.interface', { 'body': JSON.stringify(data) }).subscribe(
        (resultData) => {
          // debugger;
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);

              if(this.parmas['title'] === '拆检超时-基地' || this.parmas['title'] === '拆检超时-门店') {
                this.allList = data;
                this.allData =  data['MX'];
              }else {
                for (let i = 0, len = data.length; i < len; i++) {
                  data[i]['NKZHL'] = data[i]['NKZHL'] + '%';
                  data[i]['SL'] = data[i]['SL'] + '台';
                  data[i]['JCSL'] = data[i]['JCSL'] + '台';
                }
                this.allData = data;
              }

              this.allData = this.allData.sort(this.sortarr("JCBEYONDTIME"));
              this.allData = this.allData.sort(this.sortarr("SJ"));
              this.allData = this.allData.sort(this.sortarr("SC"));
              this.allData = this.allData.sort(this.sortarr("SL"));
              this.pageList(this.allData);
            }
          } else {
            this.msg.info(result.msg);
          }
        });

    }
  }

  // 分页显示
  pageList(arrData: any[]) {
    const count = arrData.length;
    this.reportData = [];
    let tempIndex = 0;
    for (let i = this.tablesIndex; i < count; i++) {
      if (tempIndex < 4) {
        this.reportData.push(arrData[i]);
      } else {
        break;
      }
      tempIndex++;
      this.tablesIndex++;
    }
    //当数据遍历完 重新请求数据
    if (this.tablesIndex >= count) {
      this.tablesIndex = 0;
    }
  }

  // 显示 天 时 分 换算
  toHourMinute(minutes) {
    if (Math.floor(minutes / 60 / 24) >= 1) {
      if (Math.floor(minutes / 60 / 24) < 10) {
        return (Math.floor(minutes / 60 / 24) + "天");
      }
      return (Math.floor(minutes / 60 / 24) + "天");
    } else if (Math.floor(minutes / 60 / 24) < 1) {
      if (Math.floor(minutes) < 60) {
        return (Math.floor(minutes % 60) + "分钟");
      }
      return (Math.floor(minutes / 60) + "小时");
    }

  }

  // 排序 倒序
  sortarr(key) {
    return function (a, b) {
      a[key] == undefined ? a[key] = 0 :
        b[key] == undefined ? b[key] = 0 : 0 ;
      return b[key] - a[key];
    };
  }
}
